/**
 入驻小区
 **/
(function (vc) {
    vc.extends({
        data: {
            parkingAreaTotalControlVideoInfo: {
                paId: '',
                machines: [],
                jessibuca:{},
            }
        },
        _initMethod: function () {

            let _paId = vc.getParam('paId');

            if (_paId) {
                $that.parkingAreaTotalControlVideoInfo.paId = _paId;
                $that._listMachines();
            }

        },
        _initEvent: function () {
            vc.on('parkingAreaTotalControlVideo', 'switch', function (param) {
                // if (param.hasOwnProperty('paId')) {
                //     $that.parkingAreaTotalControlVideoInfo.paId = param.paId;
                //     $that._listMachines();
                // }
            });

            vc.on('parkingAreaTotalControlVideo', 'notify', function (_data) {
                $that._showCarInoutMachineInoutInfo(_data);
                $that._showCarInoutMachineImgInfo(_data);

                let _machines = $that.parkingAreaTotalControlVideoInfo.machines;

                _machines.forEach(item => {
                    if (item.machineId == _data.machineId) {
                        vc.emit('parkingAreaTotalControlFee', 'notify', {
                            machine: item,
                            data: _data
                        });
                    }
                })


            });

        },
        methods: {
            _showCarInoutMachineImgInfo: function (_data) {
                if (_data.action != 'IN_OUT') {
                    return;
                }
                let _machines = $that.parkingAreaTotalControlVideoInfo.machines;

                _machines.forEach(item => {
                    if (item.machineId == _data.machineId) {
                        setTimeout(function () {
                            item.inOutImg = _data.img.replace('.jpg', '_plate.jpg');
                            //强制刷新下
                            $that.$forceUpdate();
                        }, 1500);
                    }
                })
            },
            _showCarInoutMachineInoutInfo: function (_data) {
                if (_data.action != 'FEE_INFO') {
                    return;
                }
                let _machines = $that.parkingAreaTotalControlVideoInfo.machines;
                _machines.forEach(item => {
                    if (item.machineId == _data.machineId) {
                        item.carNum = _data.carNum;
                        item.inOutTime = _data.inOutTime;
                        item.open = _data.open;
                        item.openMsg = _data.remark;
                    }
                })
            },
            _listMachines: function () {
                let param = {
                    params: {
                        paId: $that.parkingAreaTotalControlVideoInfo.paId,
                        page: 1,
                        row: 100,
                        communityId: vc.getCurrentCommunity().communityId
                    }
                }
                //发送get请求
                vc.http.apiGet('/barrier.listBarrier',
                    param,
                    function (json, res) {
                        let _machineManageInfo = JSON.parse(json);
                        let _machines = _machineManageInfo.data;
                        _machines.forEach(item => {
                            item.carNum = '';
                            item.inOutTime = '';
                            item.open = '';
                            item.openMsg = '';
                        })
                        $that.parkingAreaTotalControlVideoInfo.machines = _machines;

                        // 初始化视频
                        $that.initMachineVedio();
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            initMachineVedio: function () {

                let _machines = $that.parkingAreaTotalControlVideoInfo.machines;
                let wsUrl = "";
                setTimeout(function () {
                    _machines.forEach(item => {
                        $that._swatchVedio(item);
                    })
                }, 2000)


            },
            _swatchVedio: function (_machine) {
                //创建一个socket实例
                let wsUrl = "";

                wsUrl = _machine.machineIp;

                wsUrl = wsUrl.replace(':8131', ':9080');
                let _protocol = window.location.protocol;

                if (wsUrl.indexOf(":") < 0) {
                    wsUrl = wsUrl + ":9080";
                }

                wsUrl += "/ws.flv";
                wsUrl =
                    "ws://" + wsUrl;
                $that._showCameraVideo(wsUrl,"receiverDiv" + _machine.machineId)
            },
            _showCameraVideo: function (wsUrl, _div) {
                let image = document.getElementById(_div);
                let jMap = $that.parkingAreaTotalControlVideoInfo.jessibuca;
                let jessibuca = jMap[_div];
                if (jessibuca) {
                    jessibuca.destroy();
                }
                jessibuca = new Jessibuca({
                    container: image,
                    videoBuffer: 0.2, // 缓存时长
                    isResize: false,
                    text: "",
                    loadingText: "",
                    useMSE: false,
                    debug: false,
                    isNotMute: false,
                },);

                jessibuca.play(wsUrl);

                jMap[_div] = jessibuca;

            },
            _openDoor: function (_machine) {

                let _data = {
                    "machineCode": _machine.machineCode,
                    "stateName": "开门",
                    "state": "1500",
                    "url": "/barrier.openBarrierDoor",
                    "userRole": "staff",
                    "communityId": vc.getCurrentCommunity().communityId
                };
                vc.http.apiPost('/barrier.openBarrierDoor',
                    JSON.stringify(_data), {
                    emulateJSON: true
                },
                    function (json, res) {
                        let _data = JSON.parse(json);
                        if (_data.code != 0) {
                            vc.toast(_data.msg);
                        } else {
                            vc.toast('已请求设备');
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(json);
                    });
            },
            _closeDoor: function (_machine) {
                let _data = {
                    "machineCode": _machine.machineCode,
                    "stateName": "关门",
                    "state": "1500",
                    "url": "/barrier.closeBarrierDoor",
                    "userRole": "staff",
                    "communityId": vc.getCurrentCommunity().communityId
                };
                vc.http.apiPost('/barrier.closeBarrierDoor',
                    JSON.stringify(_data), {
                    emulateJSON: true
                },
                    function (json, res) {
                        let _data = JSON.parse(json);
                        if (_data.code != 0) {
                            vc.toast(_data.msg);
                        } else {
                            vc.toast('已请求设备');
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(json);
                    });
            },
            customCarIn: function (_machine, _type) {
                vc.emit('parkingAreaControlCustomCarInout', 'open', {
                    type: _type,
                    machineId: _machine.machineId,
                    boxId: _machine.locationObjId,
                    paId: $that.parkingAreaTotalControlVideoInfo.paId
                })
            },
            openCloudFlvViedo: function (_machineId) {
                let _param = {
                    params: {
                        "machineId": _machineId,
                        "communityId": vc.getCurrentCommunity().communityId
                    }

                };
                vc.http.apiGet('/barrier.getCloudFlvVideo',
                    _param,
                    function (json, res) {
                        let _data = JSON.parse(json);
                        if (_data.code != 0) {
                            vc.toast(_data.msg);
                            return;
                        }
                        console.log(_data)
                        $that._showCameraVideo(_data.data.url, 'receiverDiv'+_machineId);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(json);
                    });
            }
        }
    });
})(window.vc);